package com.example.asus.databasedemo1;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements BaseColumns {

    private Button btn;
    private Button btn_show;
    private RadioGroup rg;
    private EditText et_1;
    private EditText et_2;
    private TextView tv;
    private TextView tv_1;
    private TextView tv_2;
    private ContactCAO cao = new ContactCAO(this);

    private MODE mode = MODE.INSERT;

    private final static String ELEMENT = "element";
    private final static String ELEMENT_CONTENT = "element_content";
    private final static String CARDNAME = "cardName";
    private final static String COST = "cost";
    private final static String SYMBOL = ": ";

    enum MODE {INSERT, UPDATE, DELETE, SELECT}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_1.getText())) {
                    et_1.setError("该输入框不能为空");
                    return;
                }
                if (TextUtils.isEmpty(et_2.getText())) {
                    et_2.setError("该输入框不能为空");
                    return;
                }

                String str1 = et_1.getText().toString();
                String str2 = et_2.getText().toString();
                switch (mode) {
                    case INSERT:
                        cao.insertData(str1, str2);
                        break;
                    case DELETE:
                        cao.deleteData(str1, str2);
                        break;
                    case SELECT:
                        Cursor cursor = cao.queryData(str1, str2);
                        cursor.moveToFirst();
                        String str = "列表：\n";
                        if (cursor.getCount() == 0) {
                            Log.i("TAG", "no data");
                            tv.setText(str + "no data");
                            return;
                        }
                        do {
                            str = str + cursor.getString(cursor.getColumnIndex(_ID)) + " "
                                    + cursor.getString(cursor.getColumnIndex(MyDbHelper._CARDNAME)) +
                                    " " + cursor.getString(cursor.getColumnIndex(MyDbHelper._COST)) + "\n";
                        }
                        while (cursor.moveToNext());
                        cursor.close();
                        tv.setText(str);

                        break;
                    case UPDATE:
                        cao.updateData(str1, str2);
                        break;
                    default:
                        break;
                }
            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_delete:
                        mode = MODE.DELETE;
                        tv_1.setText(ELEMENT + SYMBOL);
                        et_1.setHint("Please enter " + ELEMENT);
                        tv_2.setText(ELEMENT_CONTENT + SYMBOL);
                        et_2.setHint("Please enter " + ELEMENT_CONTENT);
                        Log.i("TAG", mode.toString());
                        break;
                    case R.id.rb_insert:
                        mode = MODE.INSERT;
                        tv_1.setText(CARDNAME + SYMBOL);
                        et_1.setHint("Please enter " + CARDNAME);
                        tv_2.setText(COST + SYMBOL);
                        et_2.setHint("Please enter " + COST);
                        Log.i("TAG", mode.toString());
                        break;
                    case R.id.rb_select:
                        mode = MODE.SELECT;
                        tv_1.setText(ELEMENT + SYMBOL);
                        et_1.setHint("Please enter " + ELEMENT);
                        tv_2.setText(ELEMENT_CONTENT + SYMBOL);
                        et_2.setHint("Please enter " + ELEMENT_CONTENT);
                        Log.i("TAG", mode.toString());
                        break;
                    case R.id.rb_update:
                        mode = MODE.UPDATE;
                        tv_1.setText(CARDNAME + SYMBOL);
                        et_1.setHint("Please enter " + CARDNAME);
                        tv_2.setText(COST + SYMBOL);
                        et_2.setHint("Please enter " + COST);
                        Log.i("TAG", mode.toString());
                        break;
                }
            }
        });

        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor cursor = cao.queryData(null, null);
                cursor.moveToFirst();
                String str = "列表：\n";
                if (cursor.getCount() == 0) {
                    Log.i("TAG", "no data");
                    tv.setText(str + "no data");
                    return;
                }
                do {
                    str = str + cursor.getString(cursor.getColumnIndex(_ID)) + " " +
                            cursor.getString(cursor.getColumnIndex(MyDbHelper._CARDNAME)) +
                            " " + cursor.getString(cursor.getColumnIndex(MyDbHelper._COST)) + "\n";
                }
                while (cursor.moveToNext());
                cursor.close();
                tv.setText(str);
            }
        });
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        btn_show = (Button) findViewById(R.id.btn_show);
        tv = (TextView) findViewById(R.id.tv);
        rg = (RadioGroup) findViewById(R.id.rg);
        et_1 = (EditText) findViewById(R.id.et_1);
        et_2 = (EditText) findViewById(R.id.et_2);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
    }
}
